<?php
/**
 * Created by PhpStorm.
 * User: Rung-PC
 * Date: 2018/8/16
 * Time: 14:39
 */

namespace app\admin\controller;


use think\Controller;

class Login extends Controller{
    public function __construct(){
        parent::__construct();
        $this->view->engine->layout(false);
        if(session('?loginTime') && ((time()-session('loginTime')) < config('session.expire')) && request()->action() != 'out'){
            $this->redirect('index/member');
        }
    }

    public function index(){
        return $this->fetch();
    }

    public function login(){
        if(request()->isAjax()){
            $user_name = input('user_name');
            $password = md5(input('password'));

            $userInfo = db('admin_user')->where(['user_name'=>$user_name,'password'=>$password])->find();
            if($userInfo){
                session('loginTime',time());
                return array('status'=>1,'url'=>Url('admin/index/member'));
            }else{
                return array('status'=>0,'msg'=>'账号或密码错误!');
            }
        }
    }

    public function out(){
        session(null);
        $this->redirect('login/index');
    }
}